Welcome to Hands on deep learning!

02.03.09 矩阵-矩阵乘法

import torch

A = torch.arange(20, dtype=torch.float32).reshape(5, 4)

B = torch.ones(4, 3)

print(A)

print("*"*20)

print(B)

print("*"*30)

print(A.shape, B.shape)

print("*"*40)

print(torch.mm(A, B))

返回值:

tensor([[ 0., 1., 2., 3.],

[ 4., 5., 6., 7.],

[ 8., 9., 10., 11.],

[12., 13., 14., 15.],

[16., 17., 18., 19.]])

********************

tensor([[1., 1., 1.],

[1., 1., 1.],

[1., 1., 1.],

[1., 1., 1.]])

******************************

torch.Size([5, 4]) torch.Size([4, 3])

****************************************

tensor([[ 6., 6., 6.],

[22., 22., 22.],

[38., 38., 38.],

[54., 54., 54.],

[70., 70., 70.]])

矩阵-矩阵乘法可以简单地称为矩阵乘法,不应与“Hadamard积”混淆。